import $ from 'zepto'
import userCenterApi from 'clientApi/userCenterApi'
import clientInfo from 'gapi/clientInfo'
import widget from 'gapi/widget.js' 

export default {
    getApiPath (){
        return '/api/v2/client'
    },
    getApiPathforSaas (){
        return '/sea/api/1.0/client/v1/'
    },
    OS (){
        const os = navigator.userAgent.match(/iphone|ipad|ipod/i) ? "ios" : "android";
        return os;  
    },
    ajax (pathparam, options) {
        let apiUrl = ''
        let headers = {}
        clientInfo.getNetworkType(data => {
            if (data.network != 'invalid') {
                userCenterApi.getHost(data => {
                    apiUrl =  data.info + this.getApiPathforSaas() + pathparam
                    console.log(apiUrl)
                    clientInfo.getCommonHeaders(data => {
                        headers = data
                        return new Promise((resolve, reject) => {
                            $.ajax(options).done(resolve).fail(reject)
                        });                                        
                    }) 
                })
            }else {
                widget.toast('网络连接失败，请检查网络')
            }
        })
    },

    ajax3 ( pathparam, options, method, data) { //fixme
        let apiUrl = " http://saas-test.doumi.com/sea/api/1.0/client/v1/"

        let headers = {cookie: "doumi_melon=;", Info: "LmRlaxAK8k9wfAMjz6NisKiH5k2dq+fvpOn3kirDZwSZl3N46W4=", accessToken: "LmRlaxB+MkrRDMNGemZiZc0yRoi5PkIfEVnTRqpjYmXNIrbN/E5nGuFchnb6cyK0zFfT7Zi6"}
        // clientInfo.getNetworkType(data => {
        //     if (data.network != 'invalid') {
                // clientInfo.getHost(data => {
                    // apiUrl =  data.info + this.getApiPathforSaas() + pathparam
                    // console.log(apiUrl)
                    options['url'] = apiUrl + pathparam;
                    // clientInfo.getCommonHeaders(data => {
                        // headers = data
                        options['headers'] = headers;
                        return $.ajax(options)
                    // }) 
                // })
        //     }else {
        //         widget.toast('网络连接失败，请检查网络')
        //     }
        // })
    },
    fetch (){
        return {
            "errno": 0,
            "errmsg": "success",
            "data": [
                {
                "id": "1",
                "name": "移动校园推广项目",
                "number": "100",
                "pro_name": '斗米兼职',
                "first_msg": '考勤：同学还有10分钟就要上班了，不要忘...',
                "msg_count": 100
                },
                {
                "id": "2",
                "name": "清华大学校园中国移动微信推广",
                "number": "100",
                "pro_name": '斗米兼职',
                "first_msg": '考勤：同学还有10分钟就要上班了，不要忘...',
                "msg_count": 10
                },
                {
                "id": "3",
                "name": "发票传单派发项目",
                "number": "100",
                "pro_name": '斗米兼职',
                "first_msg": '考勤：同学还有10分钟就要上班了，不要忘...',
                "msg_count": 10
                },
                {
                "id": "4",
                "name": "华为技术部数据备份项目",
                "number": "100",
                "pro_name": '斗米兼职',
                "first_msg": '考勤：同学还有10分钟就要上班了，不要忘...',
                "msg_count": 10
                },
                {
                "id": "5",
                "name": "微信无线移动校园推广项目",
                "number": "100",
                "pro_name": '斗米兼职',
                "first_msg": '考勤：同学还有10分钟就要上班了，不要忘...',
                "msg_count": 10
                }
            ]
        }
    },
    /*读取查询字符串*/
    getQueryString:name => {
        let params = location.search.substring(1).toLowerCase();
        let paramList = [];
        let param = null;
        let parami;
        if (params.length > 0) {
            if (params.indexOf("&") >= 0) {
                paramList = params.split("&");
            } else {
                paramList[0] = params;
            }
            for (let i = 0, listLength = paramList.length; i < listLength; i++) {
                parami = paramList[i].indexOf(name + "=");
                if (parami >= 0) {
                    param = paramList[i].substr(parami + (name + "=").length);
                }
            }
        }
        return param;
    }
}